<?php
class Admin_DispatchingController extends Ctrl_Admin{
    public function indexAction(){
        $p = $_REQUEST;
        $pDepartment = empty($p['department'])?'':Tool_Fnc::safe_string($p['department']);
        $pStatusid = empty($p['statusid'])?'':Tool_Fnc::safe_string($p['statusid']);
        
        $tWhere = ' 1 ';

        if(!empty($pDepartment)){
            $tWhere .= ' and department = '.$pDepartment;
        }
        
        if(!empty($pStatusid)){
            $tWhere .= ' and status = '.$pStatusid;
        }

        $tAMO = new ApplylistModel;
        $tDMO = new DispatchingModel;
        $tAdMO = new AdminModel;
        $tWMO = new WorkerModel;
        $tDIMO = new DepartmentinfoModel;
        $pDepartment = empty($p['department'])?'':$p['department'];
        $statusid = empty($p['statusid'])?'':$p['statusid'];
        $department = $tDIMO->field('num,introduce')->fList();
        $tTime = time();


        if(!empty($_COOKIE['admin']['roles'])){
            if($_COOKIE['admin']['roles'] != 1 && $_COOKIE['admin']['roles'] != 2){//管理员之外
                $tSql = 'select w.department department from '.$tAMO->table.' a left join '.$tWMO->table.' w on w.id = a.w_id where a.id = '.$_SESSION['admin']['id'];
                $tAData = $tAMO->query($tSql);
                $tWhere = ' 1 and w.department = '.$tAData[0]['department'];
            }
        }

        if($_SERVER['REQUEST_METHOD'] == 'POST'){
            $pId = empty($p['id'])?Tool_Fnc::ajaxMsg('ID出错'):Tool_Fnc::safe_string($p['id']);
            $pAid = empty($p['aid'])?Tool_Fnc::ajaxMsg('aid出错'):Tool_Fnc::safe_string($p['aid']);

            $pAData = $tAMO->field('department')->where('apply_id = '.$pAid)->fRow();

            if(empty($pAData)){Tool_Fnc::ajaxMsg('系统错误');}

            $tData = array(
                'id' => $pId,
                'status' => 2,
                'department' => $pAData['department'],
                'updated' => $tTime, 
            );
            $tDMO->begin();
            if(!$tDMO->update($tData)){
                Tool_Fnc::ajaxMsg('操作失败');
            }
            //更新采购清单状态
            $tData = array(
                'apply_id' => $pAid,
                'sta_id' => 4,
            );
            if(!$tAMO->update($tData)){
                $tDMO->back();
                Tool_Fnc::ajaxMsg('操作失败 002');
            }
            $tDMO->commit();
            Tool_Fnc::ajaxMsg('操作成功',1);
        }
        $tCount = $tDMO->field('count(id) c')->where($tWhere)->fList();
        $tCnt = empty($tCount[0]['c'])?0:$tCount[0]['c'];
        $tLimit = 10;
        $tPage = new Tool_Page($tCnt,$tLimit);

        $tDatas = array();
        if(!empty($tCnt)){
            $tDatas = $tDMO->field('id,a_id,status,department')->where($tWhere)->order('id desc')->limit($tPage->limit())->fList();
            if(!empty($tDatas)){
                foreach($tDatas as $tK=>$tV){
                    $tAData = $tAMO->field('id,apply_name,apply_num,apply_time,apply_com')->where('apply_id = '.$tV['a_id'])->fRow();
                    $tDatas[$tK]['apply_name'] = $tAData['apply_name'];
                    $tDatas[$tK]['apply_num'] = $tAData['apply_num'];
                    $tDatas[$tK]['apply_time'] = $tAData['apply_time'];
                    $tDatas[$tK]['apply_com'] = $tAData['apply_com'];
                    //获取部门
                    //$tSql = 'select introduce department from '.$tDIMO->table.' di on w.department = di.num where ad.id = '.$tAData['id'].' and '.$tWhere;
                    $tDIData = $tDIMO->field('introduce department')->where('num = '.$tV['department'])->fRow();
                    $tDatas[$tK]['department'] = $tDIData['department'];
                }
            }
        }

        //部门显示
        $tDIDatas = $tDIMO->field('*')->fList();

        $this->assign('tDIDatas',$tDIDatas);
        $this->assign('tShow',$tPage->show());
        $this->assign('tDatas',$tDatas);
        $this->assign('pDepartment',$pDepartment);
        $this->assign('department',$department);
        $this->assign('statusid',$statusid);
    }
}
?>
